/**
 * 添加或者修改页面
 */
var ProductOrderInfoDlg = {
    data: {
        product:{
            id: "",
            name: ""
        },
        color:{
            id:"",
            name:""
        },
        material: "",
        unit: "",
        price: "",
        packageSize: "",
        unitMin: "",
        unitMax: "",
        unitVolume: "",
        unitWeight: "",
        timeToMarket: "",
        channel: "",
        reportCertificate: "",
        minOrderAmount: "",
        minSellingPrice: "",
        uniformSalesPrice: "",
        conversionRatio: "",
        size: "",
        productOrderName: "",
        bothfee: "",
        remark: "",
        createTime: "",
        createUser: "",
        updateTime: "",
        kindId: "",
        twenty: "",
        forty: "",
        fortyHQ: ""
    }
};

layui.use(['form', 'admin', 'ax', 'upload','laydate'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var upload = layui.upload;
    var laydate = layui.laydate;

    //让当前iframe弹层高度适应
    admin.iframeAuto();


    laydate.render({
        elem: '#timeToMarket' //指定元素
    });




    $("#name_1").click(function () {
        var formName = encodeURIComponent("parent.ProductOrderInfoDlg.data.product.name");
        var formId = encodeURIComponent("parent.ProductOrderInfoDlg.data.product.id");
        var treeUrl = encodeURIComponent(Feng.ctxPath + "/classification/getTree");

        layer.open({
            type: 2,
            title: '分类选择',
            area: ['300px', '350px'],
            content: Feng.ctxPath + '/system/commonTree?formName=' + formName + "&formId=" + formId + "&treeUrl=" + treeUrl,
            end: function () {
                $("#classification").val(ProductOrderInfoDlg.data.product.id);
                $("#name_1").val(ProductOrderInfoDlg.data.product.name);
            }
        });
    });

    $("#colorCategory").click(function () {
        var formName = encodeURIComponent("parent.ProductOrderInfoDlg.data.color.name");
        var formId = encodeURIComponent("parent.ProductOrderInfoDlg.data.color.id");
        var treeUrl = encodeURIComponent(Feng.ctxPath + "/color/getTree");

        layer.open({
            type: 2,
            title: '颜色选择',
            area: ['300px', '350px'],
            content: Feng.ctxPath + '/system/commonTree?formName=' + formName + "&formId=" + formId + "&treeUrl=" + treeUrl,
            end: function (data) {
                $("#color").val(ProductOrderInfoDlg.data.color.id);
                    var ajax = new $ax(Feng.ctxPath + "/productOrder/colorDetail/"+ProductOrderInfoDlg.data.color.id,function (data) {
                        console.log(data.data);
                        $("#colorCategory").val(data.data);
                    }, function (data) {
                        Feng.error("查询失败！" + data.message)
                    });
                    ajax.start();
            }
        });
    });

    /*回显颜色*/


    //表单提交事件
    form.on('submit(btnSubmit)', function (data) {

        var ajax = new $ax(Feng.ctxPath + "/productOrder/addItem", function (data) {
            Feng.success("添加成功！");
            window.location.href = Feng.ctxPath + '/productOrder'
        }, function (data) {
            Feng.error("添加失败！" + data.responseJSON.message)
        });
        ajax.set(data.field);
        ajax.start();

        return false;
    });

    //返回按钮
    $("#backupPage").click(function () {
        window.location.href = Feng.ctxPath + '/productOrder'
    });


    var uploadView = $('#imageList')
        , uploadListIns = upload.render({
        elem: '#selectFile'
        , url: Feng.ctxPath + '/productPic/upload'
        , accept: 'image'
        , multiple: true
        , auto: false
        , bindAction: '#start'
        , choose: function (obj) {
            var files = this.files = obj.pushFile(); //将每次选择的文件追加到文件队列
            //读取本地文件
            console.log("00000");
            obj.preview(function (index, file, result) {
                var tr = $(['<tr id="upload-' + index + '">'
                    , '<td>' + file.name + '</td>'
                    , '<td>' + '<img src="' + result + '" alt="' + file.name + '" class="layui-upload-img">' + '</td>'
                    , '<td>等待上传</td>'
                    , '<td>'
                    , '<button class="layui-btn layui-btn-xs demo-reload layui-hide">重传</button>'
                    , '<button class="layui-btn layui-btn-xs layui-btn-danger demo-delete">删除</button>'
                    , '</td>'
                    , '</tr>'].join(''));

                //单个重传
                tr.find('.demo-reload').on('click', function () {
                    obj.upload(index, file);
                });

                //删除
                tr.find('.demo-delete').on('click', function () {
                    delete files[index]; //删除对应的文件
                    tr.remove();
                    uploadListIns.config.elem.next()[0].value = ''; //清空 input file 值，以免删除后出现同名文件不可选
                });

                uploadView.append(tr);
            });
        },
        done: function (res, index, upload) {
            // console.log("=======",res.data);
            var str = res.data;
            //遍历picPath的所有input元素
            $("#picPath").each(function () {
                str += ","+$(this).val();
            });
            console.log("图",str);
            if (res.code === 200) { //上传成功
                // $("#picPath").val($("#picPath").val() + res.data + ",");
                $("#picPath").val(str);
                // 将数据放到input框中
                var tr = uploadView.find('tr#upload-' + index)
                    , tds = tr.children();
                tds.eq(2).html('<span style="color: #5FB878;">上传成功</span>');
                tds.eq(3).html(''); //清空操作
                return delete this.files[index]; //删除文件队列已经上传成功的文件
            }
            this.error(index, upload);
        }
        , error: function (index, upload) {
            var tr = uploadView.find('tr#upload-' + index)
                , tds = tr.children();
            tds.eq(2).html('<span style="color: #FF5722;">上传失败</span>');
            tds.eq(3).find('.demo-reload').removeClass('layui-hide'); //显示重传
        }
    });






    //产品附件上传
    var uploadView2 = $('#imageList2')
        , uploadListIns2 = upload.render({
        elem: '#selectFile2'
        , url: Feng.ctxPath + '/zApplication/UploadFileShi'
        , accept: 'file'
        , multiple: true
        , auto: false
        , bindAction: '#start2'
        , choose: function (obj) {
            var files = this.files = obj.pushFile(); //将每次选择的文件追加到文件队列
            //读取本地文件
            obj.preview(function (index, file, result) {
                var tr = $(['<tr id="upload-' + index + '">'
                    , '<td>' + file.name + '</td>'
                    , '<td>' + '<img src="' + result + '" alt="' + file.name + '" class="layui-upload-img">' + '</td>'
                    , '<td>等待上传</td>'
                    , '<td>'
                    , '<button class="layui-btn layui-btn-xs demo-reload layui-hide">重传</button>'
                    , '<button class="layui-btn layui-btn-xs layui-btn-danger demo-delete">删除</button>'
                    , '</td>'
                    , '</tr>'].join(''));

                //单个重传
                tr.find('.demo-reload').on('click', function () {
                    obj.upload(index, file);
                });

                //删除
                tr.find('.demo-delete').on('click', function () {
                    delete files[index]; //删除对应的文件
                    tr.remove();
                    uploadListIns.config.elem.next()[0].value = ''; //清空 input file 值，以免删除后出现同名文件不可选
                });

                uploadView2.append(tr);
            });
        }
        , done: function (res, index, upload) {
            console.log($("#picPath").val());
            if (res.code === 200) { //上传成功
                $("#accessoryPath").val(res.data);
                // 将数据放到input框中
                var tr = uploadView2.find('tr#upload-' + index)
                    , tds = tr.children();
                tds.eq(2).html('<span style="color: #5FB878;">上传成功</span>');
                tds.eq(3).html(''); //清空操作
                return delete this.files[index]; //删除文件队列已经上传成功的文件
            }
            this.error(index, upload);
        }
        , error: function (index, upload) {
            var tr = uploadView2.find('tr#upload-' + index)
                , tds = tr.children();
            tds.eq(2).html('<span style="color: #FF5722;">上传失败</span>');
            tds.eq(3).find('.demo-reload').removeClass('layui-hide'); //显示重传
        }
    });




























});